#!/usr/bin/python
# encoding=utf-8


from wm.lib.aisec import AScanner
from wm.etc.config import ASCONFIG
from wm.lib.loglib import log_error
from wm.db.TransactionManager import TransactionManager

def addScaningTask(url, websiteid):
    
    tran = TransactionManager()
    try:
        tran.startTransaction()
        
        scanner = AScanner(ASCONFIG['USERNAME'],ASCONFIG['PASSWORD'])
        taskid = scanner.addTask(url)
        sql = "insert into vulscantask("+\
                             "id,createDate,modifyDate,taskID,entry,callback,websiteConfigInfo_id) "+\
                             "values(replace(uuid(),'-','')," + "CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()" +\
                             "," + str(taskid) + "," + url + ",'"+ ASCONFIG['CALLBACK'] + "','" + websiteid + "')"    
        tran.execute(sql)
        tran.commitTransaction()
    except Exception as e:
        log_error("addScaningTask():"+str(e))
        tran.rollbackTransaction()
    finally:
        tran.close()